package com.gitee.mars.tourserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.tourserver.entity.TourRoute;
import com.gitee.mars.tourserver.manager.TourClawerManager;
import com.gitee.mars.tourserver.mapper.TourRouteMapper;
import com.gitee.mars.tourserver.request.TourRouteRequest;
import com.gitee.mars.tourserver.response.base.PageInfo;
import com.gitee.mars.tourserver.service.ITourRouteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * TourRoute 实现层
 *
 * @Author wq
 * @Date 2021-04-14
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class TourRouteServiceImpl extends ServiceImpl<TourRouteMapper, TourRoute> implements ITourRouteService {

    @Resource
    private TourClawerManager clawerManager;

    @Override
    public void acquire() {
        List<TourRoute> tourRoute = clawerManager.getTourRoute();
        tourRoute.forEach(this::save);
    }

    @Override
    public PageInfo<TourRoute> list(TourRouteRequest request) {
        IPage<TourRoute> page = new Page<>(request.getPageNumber(), request.getPageSize());

        LambdaQueryWrapper<TourRoute> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getName())) {
            wrapper.like(TourRoute::getName, request.getName());
        }
        if (StringUtils.isNotEmpty(request.getTitle())) {
            wrapper.like(TourRoute::getTitle, request.getTitle());
        }
        wrapper.orderByDesc(TourRoute::getCreateTime);
        List<TourRoute> list = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(page.getTotal(), list);
        }
        return null;
    }
}